#include "form.h"
#include "ui_form.h"
#include <QMouseEvent>
#include<QDebug>
#include <QFont>
#include <QMessageBox>
#include <QInputDialog>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlResult>

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    m_list_1 = new food_list;
    m_list_2 = new Food_list_2;
    m_list_3 = new Food_list_3;

    connect(m_list_1, &food_list::sendInt, this,
            &Form::recvInt);
    connect(m_list_2, &Food_list_2::sendInt, this,
            &Form::recvInt);
    connect(m_list_3, &Food_list_3::sendInt, this,
            &Form::recvInt);

    connect(this,&Form::starting,m_list_1,
            &food_list::sendStr);
    connect(this,&Form::starting,m_list_2,
            &Food_list_2::sendStr);
    connect(this,&Form::starting,m_list_3,
            &Food_list_3::sendStr);


    ui->label->setStyleSheet("color: red; font-size: 16px; font-family: Arial;");
    QFont font;
    //    font.setFamily("Arial"); // 设置字体家族为Arial
    //    font.setPointSize(16);   // 设置字体大小为16点
    font.setBold(true);      // 设置字体为粗体
    ui->label->setFont(font);
    ui->label->setAlignment(Qt::AlignCenter); // 设置文本居中

    ui->label_2->setStyleSheet("color: red; font-size: 16px; font-family: Arial;");
    ui->label_2->setFont(font);
    ui->label_2->setAlignment(Qt::AlignCenter); // 设置文本居中

    ui->label_3->setStyleSheet("color: red; font-size: 16px; font-family: Times New Roman;");
    ui->label_3->setFont(font);
    ui->label_3->setAlignment(Qt::AlignCenter); // 设置文本居中

    // 安装事件过滤器
    this->installEventFilter(this);

    QPixmap pix1_1(":/res/1-1.png");
    pix1_1 = pix1_1.scaled(400,210);
    ui->lb_1->setPixmap(pix1_1);
    ui->lb_1->installEventFilter(this); // 安装事件过滤器到lb_1

    QPixmap pix1_2(":/res/1-2.png");
    pix1_2 = pix1_2.scaled(400,210);
    ui->lb_2->setPixmap(pix1_2);
    ui->lb_2->installEventFilter(this); // 安装事件过滤器到lb_2

    QPixmap pix1_3(":/res/1-3.png");
    pix1_3 = pix1_3.scaled(400,210);
    ui->lb_3->setPixmap(pix1_3);
    ui->lb_3->installEventFilter(this); // 安装事件过滤器到lb_3




}

void Form::recvInt(int i)
{
    if( i == 1 )
    {
        this->show();
    }
    if( i == 2)
    {
        this->close();
    }

}

Form::~Form()
{
    delete ui;
}

bool Form::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (mouseEvent->button() == Qt::LeftButton)
        {
            if (obj == ui->lb_1)
            {
                m_list_1->show();
                this->hide();
            }
            else if (obj == ui->lb_2)
            {
                m_list_2->show();
                this->hide();
            }
            else if (obj == ui->lb_3)
            {
                m_list_3->show();
                this->hide();
            }
            return true; // 表明事件已被处理
        }
    }
    // 如果事件不是鼠标左键点击，则继续正常的事件处理
    return QObject::eventFilter(obj, event);

}



void Form::on_bt_clicked()
{
    int ret = 0;
    ret = QMessageBox::question(this, "询问", "你确定要退出吗",
                                QMessageBox::No | QMessageBox::Yes,
                                QMessageBox::No);
    if(ret == QMessageBox::Yes)
    {

        this->close();
    }

}

void Form::on_bt_2_clicked()//选择地址
{

    //设置驱动
    m_db = QSqlDatabase::addDatabase("QODBC");
    QString dsn = "Driver={MySQL ODBC 8.0 ANSI Driver}";
    m_db.setDatabaseName(dsn);

    //连接数据库
    m_db.setHostName("localhost");
    m_db.setPort(3306);
    m_db.setUserName("root");
    m_db.setPassword("root");

    bool ret = m_db.open();
    if(ret == false)
    {
        qDebug() << "connect err:"<< m_db.lastError().text();
        return ;
    }

    qDebug()<<"connect ok";

    QString qtn("site");
    //进入数据库
    QSqlQuery query(m_db);

    QString qlk = QString("use testDB");
    int ret1 = query.exec(qlk);
    if(ret1 == false)
    {
        qDebug()<<"use testDB err"<<query.lastError().text();
    }

    //从数据库中读取数据
    QString sql = QString("select site from %1").arg(qtn);
    bool ret2 = query.exec(sql);
    if(ret2 == false)
    {
        qDebug()<<"select err"<<query.lastError().text();
    }


    QString str;
    int nField = query.record().count();

    QStringList sl;
    while(query.next())
    {
        for(int i = 0; i<nField; i++)
        {
            str = query.value(i).toString();
            sl << str;
        }
    }

    //写到mysql中
    bool ok;

    str1 = QInputDialog::getItem(this,"地址",
                                 "请选择你的送货地址",sl, 0, true, &ok);
    if(ok == true)
    {
        qDebug()<<"地址:"<<str1;
        emit starting(str1);
    }

    m_db.close();  //清理工作

}

void Form::on_bt_3_clicked()//新增地址
{
    //设置驱动
    m_db = QSqlDatabase::addDatabase("QODBC");
    QString dsn = "Driver={MySQL ODBC 8.0 ANSI Driver}";
    m_db.setDatabaseName(dsn);

    //连接数据库
    m_db.setHostName("localhost");
    m_db.setPort(3306);
    m_db.setUserName("root");
    m_db.setPassword("root");

    bool ret = m_db.open();
    if(ret == false)
    {
        qDebug() << "connect err:"<< m_db.lastError().text();
        return ;
    }

    qDebug()<<"connect ok";

    QString qtn("site");
    //进入数据库
    QSqlQuery query(m_db);

    QString qlk = QString("use testDB");
    int ret1 = query.exec(qlk);
    if(ret1 == false)
    {
        qDebug()<<"use testDB err"<<query.lastError().text();
    }


    bool ok;
    QStringList sl;
    str1 = QInputDialog::getItem(this,"地址",
                                 "请选择你的送货地址",sl, 0, true, &ok);
    qDebug() << "新地址:" << str1;

    //表中插入数据
    QString sql = QString("insert into %1 (site) values ('%2')").arg(qtn).arg(str1);
    bool ret2 = query.exec(sql);
    if(ret2 == false)
    {
        int err = query.lastError().nativeErrorCode().toInt();
        if(err != 1062) //主键重复
            qDebug()<<"insert table err"<<query.lastError().text();
        QMessageBox::warning(this,"警告","账户已存在!");
    }
    if(ret2 == true)
    {
        QMessageBox::information(this,"通知","新增地址成功");
    }
    m_db.close();  //清理工作

}
